package com.ihisomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ihisomp.base.Page;
import com.ihisomp.base.Result;
import com.ihisomp.dto.SysPathTreeDto;
import com.ihisomp.entity.SysPagePath;
import com.ihisomp.mapper.SysPagePathMapper;
import com.ihisomp.service.ISysPagePathService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 机构菜单维护 服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-04-06
 */
@Service
public class SysPagePathServiceImpl extends ServiceImpl<SysPagePathMapper, SysPagePath> implements ISysPagePathService {

    /**
     * 程序树形集合数据查询
     *
     * @return
     */
    @Override
    public Result getPathTree() {
        List<SysPathTreeDto> list = baseMapper.getPathTree();
        if (list.size() <= 0) {
            return Result.error("没有找到程序路径！");
        }
        List<SysPathTreeDto> pathList = new ArrayList();

        // 查找一级菜单路径
        for (int i = 0; i < list.size(); i++) {
            // 一级菜单没有pid
            if (StringUtils.isBlank(list.get(i).getPid())) {
                pathList.add(list.get(i));
            }
        }
        // 递归查询子菜单
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (SysPathTreeDto path : pathList) {
            path.setChildren(getChild(path.getId(), list));
        }
        Map<String, List> map = new HashMap<>();
        map.put("path", pathList);

        // 增加根节点
        SysPathTreeDto root = new SysPathTreeDto();
        root.setId("ROOT");
        root.setName("系统路径");
        root.setChildren(pathList);
        List<SysPathTreeDto> pathRoot = new ArrayList();
        pathRoot.add(root);
        return Result.ok(pathRoot);
    }

    /**
     * 递归查找子菜单
     *
     * @param id
     * @param rootPath
     * @return
     */
    private List<SysPathTreeDto> getChild(String id, List<SysPathTreeDto> rootPath) {
        // 子菜单
        List<SysPathTreeDto> childList = new ArrayList<>();
        for (SysPathTreeDto path : rootPath) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (StringUtils.isNotBlank(path.getPid())) {
                if (path.getPid().equals(id)) {
                    childList.add(path);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (SysPathTreeDto path : childList) {
            // 没有url子菜单还有子菜单
            if (StringUtils.isNotBlank(path.getUrl())) {
                // 递归
                path.setChildren(getChild(path.getId(), rootPath));
            }
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    /**
     * 查询文件夹及程序
     *
     * @return
     */
    @Override
    public Result listSelect() {
        QueryWrapper<SysPagePath> queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", 1);
        queryWrapper.ne("type", 3);
        queryWrapper.orderByAsc("seq");
        List<SysPagePath> list = baseMapper.selectList(queryWrapper);
        SysPagePath path = new SysPagePath();
        path.setId(null);
        path.setName("系统路径");
        list.add(0, path);
        return Result.ok(list);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param req
     * @return
     */
    @Override
    public Result search(long page, long size, SysPathTreeDto req) {
        if (req == null) {
            req = new SysPathTreeDto();
        }
        IPage<SysPagePath> data = baseMapper.searchPage(new Page<SysPagePath>(page, size), req);
        return Result.ok(data);
    }

    /**
     * 新增数据
     *
     * @param sysPathTreeDto
     * @return
     */
    @Override
    public Result add(SysPathTreeDto sysPathTreeDto) {
        if (sysPathTreeDto == null || StringUtils.isBlank(sysPathTreeDto.getId())) {
            return Result.error("程序路径编码不能为空！");
        }
        if (StringUtils.isBlank(sysPathTreeDto.getName())) {
            return Result.error("程序路径名称不能为空！");
        }
        if (baseMapper.selectById(sysPathTreeDto.getId()) != null) {
            return Result.error("程序路径已经存在！");
        }
        SysPagePath path = new SysPagePath();
        BeanUtils.copyProperties(sysPathTreeDto, path);
        if (baseMapper.insert(path) > 0) {
            return Result.ok();
        }
        return Result.error("新增程序路径失败！");
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @Override
    public Result delete(String id) {
        SysPagePath sysPagePath = baseMapper.selectById(id);
        if (sysPagePath == null) {
            return Result.error("无查询程序路径信息");
        }
        // 查询该程序路径是否存在子节点
        QueryWrapper<SysPagePath> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid", id);
        List<SysPagePath> list = baseMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return Result.error("该程序路径存在子节点，请先删除子节点数据！");
        }
        // 删除操作
        if (baseMapper.deleteById(id) > 0) {
            return Result.ok();
        }
        return Result.error("删除程序路径失败！");
    }

    /**
     * 更新数据
     *
     * @param id
     * @param sysPathTreeDto
     * @return
     */
    @Override
    public Result update(String id, SysPathTreeDto sysPathTreeDto) {
        SysPagePath sysPagePath = new SysPagePath();
        BeanUtils.copyProperties(sysPathTreeDto, sysPagePath);
        if (sysPagePath.getId() == null) {
            sysPagePath.setId(id);
        }
        if (baseMapper.updateById(sysPagePath) > 0) {
            return Result.ok();
        } else {
            return Result.error("更新程序路径失败！");
        }
    }

    /**
     * 查询数据
     *
     * @param id
     * @return
     */
    @Override
    public Result get(String id) {
        SysPagePath sysPagePath = baseMapper.selectById(id);
        if (sysPagePath == null) {
            return Result.error("无查询程序路径信息！");
        }
        return Result.ok(sysPagePath);
    }

    /**
     * 更新程序路径状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Result updateStatus(String id, int status) {
        SysPagePath sysPagePath = baseMapper.selectById(id);
        if (sysPagePath == null) {
            return Result.error("无查询程序路径信息");
        }
        if (status == 0) {
            sysPagePath.setStatus(1);
        } else {
            sysPagePath.setStatus(0);
        }
        if (baseMapper.updateById(sysPagePath) > 0) {
            return Result.ok();
        }
        return Result.error("更新程序路径失败！");
    }

    /**
     * 查询全部程序数据
     *
     * @return
     */
    @Override
    public Result getPagePathList() {
        List<SysPathTreeDto> list = baseMapper.getPagePathList();
        if (list.size() <= 0) {
            return Result.error("没有找到程序路径！");
        }
        List<SysPathTreeDto> pathList = new ArrayList();
        // 查找一级菜单路径
        for (int i = 0; i < list.size(); i++) {
            // 一级菜单没有pid
            if (StringUtils.isBlank(list.get(i).getPid())) {
                pathList.add(list.get(i));
            }
        }
        // 递归查询子菜单
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (SysPathTreeDto path : pathList) {
            path.setChildren(getChild(path.getId(), list));
        }
        Map<String, List> map = new HashMap<>();
        map.put("path", pathList);
        // 增加根节点
        SysPathTreeDto root = new SysPathTreeDto();
        root.setId("ROOT");
        root.setName("系统路径");
        root.setChildren(pathList);
        List<SysPathTreeDto> pathRoot = new ArrayList();
        pathRoot.add(root);
        return Result.ok(pathRoot);
    }
}
